#include "ResourceManager.h"

ResourceManager::ResourceManager(void)
{
	GraphicPackA = NULL;
	Japanese_Fonts = NULL;
}

ResourceManager::~ResourceManager(void)
{
	if (GraphicPackA != NULL)
	{
		unload_datafile(GraphicPackA);
	}
	if (Japanese_Fonts != NULL)
	{
		unload_datafile(Japanese_Fonts);
	}
	for (int i = 0; i < num_buttons; i++)
	{
		if (buttons[i].B_Hit != NULL)
		{
			release_bitmap(buttons[i].B_Hit);
		}
		if (buttons[i].B_Scratch != NULL)
		{
			release_bitmap(buttons[i].B_Scratch);
		}
	}
}

void ResourceManager::loadGlobalResources()
{
	set_uformat(U_UTF8);

	loadmsg();

	//Graphics package for the gui
	GraphicPackA = load_datafile("data/GraphicPackA.dat");
	Japanese_Fonts = load_datafile("data/japfont.dat");

	//Setup Extra Stuff
	setup();

	//Japanese Fonts
	font = (FONT *)Japanese_Fonts[0].dat;

	// Setup Butons
	for(int i = 0 ; i < num_buttons; i++)
	{
		buttons[i].oldx = buttons[i].xpos = 40;
		buttons[i].oldy = buttons[i].ypos = 130 + (80*i);
		buttons[i].B_Normal		= (BITMAP *)GraphicPackA[Folderclose].dat;
		buttons[i].B_Rollover	= (BITMAP *)GraphicPackA[Folderopen].dat; 
		buttons[i].B_Hit		= NULL;
		buttons[i].B_Scratch	= NULL;
		buttons[i].options = 0;
	}

	buttons[0].options = MENUTEXTA;
	buttons[1].options = MENUTEXTB;
	buttons[2].options = MENUTEXTC;
	buttons[3].options = MENUTEXTD;
	buttons[4].options = MENUTEXTE;

	//Initialize Button Options
	for(int i = 0 ; i < num_buttons; i++)
	{
		for(int j = 0; j < buttons[i].options; j ++)
		{
			buttons[i].text_option[j] = new char[30];
			strcpy(buttons[i].text_option[j],"");
		}
	}
	//LOAD MENU TEXT
	fstream openf;
	openf.open("data/Menutext.txt",ios::in);
	openf.getline(menutext[0],256);
	int menuopnum = 0;
	while(!openf.eof())
	{
		openf.getline(menutext[menuopnum],256);
		menuopnum++;
	}
	openf.close();
	openf.clear();
	for(int i = 0; i < MENUTEXTA; i++)
		strcpy(buttons[0].text_option[i],menutext[MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTB; i++)
		strcpy(buttons[1].text_option[i],menutext[MENUTEXTA + MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTC; i++)
		strcpy(buttons[2].text_option[i],menutext[MENUTEXTA + MENUTEXTB + MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTD; i++)
		strcpy(buttons[3].text_option[i],menutext[MENUTEXTA + MENUTEXTB + MENUTEXTC + MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTE; i++)
		strcpy(buttons[4].text_option[i],menutext[MENUTEXTA + MENUTEXTB + MENUTEXTC + MENUTEXTD + MENUOPNUM+i]);

	buttons[5].xpos = 700;
	buttons[5].ypos = 20;
	buttons[5].B_Normal		= NULL;
	buttons[5].B_Rollover	= (BITMAP *)GraphicPackA[Glow].dat;

	//Initialize Buttons
	for(int i = 0; i < num_buttons; i++)
	{
		buttons[i].trans = 0;
		buttons[i].flag = 0;
		buttons[i].hide = 0;
		buttons[i].alpha = 255;
		if(buttons[i].B_Normal != NULL)
		{
			buttons[i].drawpt = buttons[i].B_Normal->h;
			buttons[i].B_Scratch = create_bitmap(buttons[i].B_Normal->h,buttons[i].B_Normal->w);
		}
		else
		{
			buttons[i].B_Scratch = create_bitmap(1,1);
		}
	}
}